﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Xyz.Web.Reservation.Repositories;
using Xyz.Web.Reservation.Models;

namespace Xyz.Web.Reservation.Controllers
{
    public class PassengerController : Controller
    {
        PassengerRepository psgRep = new PassengerRepository();
        //
        // GET: /Passenger/

        public ActionResult Index(int passengerCount)
        {
            List<PassengerModel> passengers = new List<PassengerModel>();

            for (int i = 0; i < passengerCount; i++)
            {
                passengers.Add(new PassengerModel());
            }

            PassengerListModel list = new PassengerListModel();
            list.Passengers = passengers;

            SetViewData();
            return View(list);
        }

        [HttpPost]
        public ActionResult Index(List<PassengerModel> passengers)
        {
            PassengerListModel list = new PassengerListModel();
            list.Passengers = passengers;

            Session["PassengersList"] = list.Passengers;
            
            if (ModelState.IsValid)
            {
                return RedirectToAction("Seat", "Shoppingcart");   
            }

            SetViewData();
            return View(list);
        }

        private void SetViewData()
        {
            List<TitleModel> tModels = psgRep.GetTitles().ToModel();
            ViewData["Titles"] = new SelectList(from c in tModels select c.titleCode);
        }
    } 
}
